<div id="app" v-cloak>
    <el-card class="box-card">
        <el-form class="search" :inline="true" :model="vm" ref="vm" label-position="right" label-width="90px">
            <el-form-item label="任务名称" prop="taskName">
                <el-input v-model="vm.taskName" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入任务名称"></el-input>
            </el-form-item>
            <el-form-item label="详细注释" prop="taskNote">
                <el-input v-model="vm.taskNote" @keyup.enter.native="onSearch" clearable size="mini" placeholder="请输入详细注释"></el-input>
            </el-form-item>
            <el-form-item label="任务类型" prop="taskType">
                <el-select v-model="vm.taskType" clearable size="mini" filterable placeholder="请输入任务类型">
                    <el-option key="Cron" label="Cron表达式" value="Cron"></el-option>
                    <el-option key="DelayRepeat" label="间隔重复(秒)" value="DelayRepeat"></el-option>
                    <el-option key="FixRepeat" label="绝对重复(秒)" value="FixRepeat"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否有效" prop="valid">
                <el-radio-group v-model="vm.valid" clearable size="mini">
                    <el-radio :label="true">是</el-radio>
                    <el-radio :label="false">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="开始日期" prop="startDate">
                <el-date-picker v-model="vm.startDate" clearable size="mini" placeholder="请输入开始日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
                <el-date-picker v-model="vm.endDate" clearable size="mini" placeholder="请输入结束日期" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="mini" icon="el-icon-search" @click="onSearch">搜索</el-button>
                <el-button type="warning" size="mini" icon="el-icon-refresh-left" @click="onReset('vm')">重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>

    <el-card class="box-card">
        <el-row type="flex" justify="space-between">
            <el-button-group>
                <#if token.hasRes("/ajax/wsys/taskSql/create")>
                <el-button type="success" size="mini" icon="el-icon-plus" @click="onCreate">新增</el-button>
                </#if>

                <#if token.hasRes("/ajax/wsys/taskSql/delete")>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="onBitchDelete">删除</el-button>
                </#if>
            </el-button-group>

            <el-button-group>
            <#if token.hasRes("/ajax/wsys/taskSql/template")>
                <el-tooltip effect="dark" content="Excel导入模板下载" placement="bottom">
                    <el-button size="mini" icon="el-icon-date" @click="onTemplate"></el-button>
                </el-tooltip>
            </#if>

            <#if token.hasRes("/ajax/wsys/taskSql/imports")>
                <el-tooltip effect="dark" content="Excel导入" placement="bottom">
                    <el-button size="mini" icon="el-icon-upload2" @click="onImport"></el-button>
                </el-tooltip>
            </#if>

            <#if token.hasRes("/ajax/wsys/taskSql/exports")>
                <el-tooltip effect="dark" content="Excel导出" placement="bottom">
                    <el-button size="mini" icon="el-icon-download" @click="onExport"></el-button>
                </el-tooltip>
            </#if>

                <el-tooltip effect="dark" content="刷新" placement="bottom">
                    <el-button size="mini" icon="el-icon-refresh" @click="onFind"></el-button>
                </el-tooltip>
            </el-button-group>
        </el-row>

        <el-table
                class="data"
                @selection-change="onSelectionChange"
                empty-text="无数据"
                :stripe="true"
                :data="result"
                size="mini">
            <el-table-column
                    align="center"
                    width="45"
                    type="selection">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="140"
                    prop="id"
                    label="主键">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="100"
                    prop="valid"
                    label="是否有效">
                <template slot-scope="scope">
                    <el-tag size="mini" effect="dark" type="success" v-if="scope.row.valid">有效</el-tag>
                    <el-tag size="mini" effect="dark" type="danger" v-if="!scope.row.valid">无效</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="120"
                    prop="taskName"
                    label="任务名称">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="taskNote"
                    label="详细注释">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="100"
                    prop="taskType"
                    label="任务类型">
                <template slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.taskType == 'Cron'">Cron表达式</el-tag>
                    <el-tag size="mini" v-if="scope.row.taskType == 'DelayRepeat'">间隔重复(秒)</el-tag>
                    <el-tag size="mini" v-if="scope.row.taskType == 'FixRepeat'">绝对重复(秒)</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="typeValue"
                    label="任务类型值">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="150"
                    prop="taskSql"
                    label="任务SQL">
            </el-table-column>
            <el-table-column
                    align="center"
                    min-width="140"
                    prop="createTime"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    align="center"
                    width="120"
                    fixed="right"
                    label="操作">
                <template slot-scope="scope">
                    <wb-table-ops :arg="scope.row">
                    <#if token.hasRes("/ajax/wsys/taskSql/update")>
                        <wb-table-op value="编辑" icon="el-icon-edit" @click="onEdit"></wb-table-op>
                    </#if>
                    <#if token.hasRes("/ajax/wsys/taskSql/delete")>
                        <wb-table-op value="删除" icon="el-icon-delete" @click="onDelete"></wb-table-op>
                    </#if>
                    </wb-table-ops>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                background
                v-if="vm.totalCount > vm.pageSize"
                style="margin-top: 10px"
                @current-change="onPageChange"
                @size-change="onPageSizeChange"
                :current-page="vm.pageNumber"
                :page-size="vm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="vm.totalCount">
        </el-pagination>
    </el-card>

    <el-dialog
            :fullscreen="false"
            :title="form.formTitle"
            :custom-class="'dialog'"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :modal-append-to-body="true"
            :visible.sync="form.formShow">

        <el-page-header slot="title" @back="form.formShow = false" :content="form.formTitle"></el-page-header>

        <el-form class="form" :model="form" :inline="true" :rules="formRules" ref="form" label-position="right" label-width="90px">
            <el-form-item label="任务名称" prop="taskName">
                <el-input v-model="form.taskName" clearable size="mini" placeholder="请输入任务名称"></el-input>
            </el-form-item>
            <el-form-item label="是否有效" prop="valid">
                <el-radio-group v-model="form.valid" clearable size="mini">
                    <el-radio :label="true">是</el-radio>
                    <el-radio :label="false">否</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="任务类型" prop="taskType">
                <el-select v-model="form.taskType" clearable size="mini" filterable placeholder="请输入任务类型">
                    <el-option key="Cron" label="Cron表达式" value="Cron"></el-option>
                    <el-option key="DelayRepeat" label="间隔重复(秒)" value="DelayRepeat"></el-option>
                    <el-option key="FixRepeat" label="绝对重复(秒)" value="FixRepeat"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="类型属值" prop="typeValue">
                <el-input v-model="form.typeValue" clearable size="mini" placeholder="请输入任务类型属值"></el-input>
            </el-form-item>
            <el-form-item style="width: 600px" label="详细注释" prop="taskNote">
                <el-input style="width: 504px" type="textarea" v-model="form.taskNote" clearable size="mini" placeholder="请输入详细注释"
                          maxlength="255"
                          show-word-limit></el-input>
            </el-form-item>
            <el-form-item style="width: 600px" label="任务SQL" prop="taskSql">
                <el-input style="width: 504px" type="textarea" v-model="form.taskSql" clearable size="mini" placeholder="请输入任务SQL"
                          :autosize="{ minRows: 5, maxRows: 5}"
                          maxlength="2500"
                          show-word-limit></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
			<el-button size="mini" @click="form.formShow = false">取 消</el-button>
			<el-button size="mini" type="primary" @click="onSave">保存</el-button>
		</span>
    </el-dialog>
</div>
<script>
    var app = new Vue({
        mixins: [mixinForMgr],
        el: "#app",
        data: {
            module: 'wsys',
            target: 'taskSql',
            vm: {//条件及分页参数
                taskName: null,
                taskNote: null,
                taskType: null,
                valid: null,
                startDate: null,
                endDate: null,
                pageNumber: 1,
                pageSize: 10,
                totalCount: 0,
                sortKey:'CREATE_TIME',
                sortType:'DESC'
            },
            form: {//待提交表单
                formTitle: null,
                formShow: false,
                id: null,
                taskName: null,
                taskNote: null,
                taskType: null,
                typeValue: null,
                taskSql: null,
                valid: true,
                rowVersion: null
            },
            formRules: {
                taskName: [
                    {required: true, message: '任务名称不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '任务名称长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                taskNote: [
                    {min: 1, max: 255, message: '详细注释长度在 1 到 255 个字符', trigger: 'blur'}
                ],
                taskType: [
                    {required: true, message: '任务类型不能为空', trigger: 'blur'},
                ],
                typeValue: [
                    {required: true, message: '任务类型值不能为空', trigger: 'blur'},
                    {min: 1, max: 50, message: '任务类型值长度在 1 到 50 个字符', trigger: 'blur'}
                ],
                taskSql: [
                    {required: true, message: '任务SQL不能为空', trigger: 'blur'},
                    {min: 1, max: 2500, message: '任务SQL长度在 1 到 2500 个字符', trigger: 'blur'}
                ],
                valid: [
                    {required: true, message: '是否有效不能为空', trigger: 'blur'},
                ],
            }
        },
        methods: {
            onCreate: function () {
                this.form.formTitle = "SQL任务新增";
                this.form.formShow = true;
                this.form.id = "";
                this.form.taskName = "";
                this.form.taskNote = "";
                this.form.taskType = "";
                this.form.typeValue = "";
                this.form.taskSql = "";
                this.form.valid = "";
            },
            onEdit: function (item) {
                this.form.formTitle = "SQL任务编辑";
                this.form.formShow = true;
                this.form.id = item.id;
                this.form.taskName = item.taskName;
                this.form.taskNote = item.taskNote;
                this.form.taskType = item.taskType;
                this.form.typeValue = item.typeValue;
                this.form.taskSql = item.taskSql;
                this.form.valid = item.valid;
                this.form.rowVersion = item.rowVersion;
            },
            onDelete: function (item) {
                this.$confirm('将删除该项, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    new Ajax("wsys", "taskSql").delete({id: item.id}, function (response) {
                        if (response.errors.length > 0) {
                            nav.e(response.errors[0].message);
                        } else {
                            nav.s("删除成功.");
                            this.onFind();
                        }
                    }.bind(this))
                }.bind(this)).catch(function (action) {

                });
            }
        },
        mounted: function () {
            this.onFind();
        },
    })
</script>
